import http from '@/http/interceptor'
import ciphertext from '@/utils/ciphertext-util'

let BASE_CONTENT = '/oauth'
/** 登录 **/
export function login(dataForm) {
	return http({
		url: BASE_CONTENT + '/login',
		method: 'post',
		data: http.formData({
			'userName': dataForm.userName,
			'password': ciphertext.encode(dataForm.password),
			'sourceType': dataForm.sourceType,
			'code': dataForm.autoLogin ? (dataForm.captcha === null || dataForm.captcha.length === 0 ? 'auto' : dataForm.captcha) :
				dataForm.captcha,
			'rememberMe': dataForm.autoLogin
		}),
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded'
		}
	})
}

/** 获取验证码 **/
export function getCaptcha(data) {
	return http.abortUrl(BASE_CONTENT + '/captcha', {
		'target': data.userName,
		'sourceType': data.sourceType
	})
}

/** 刷新token **/
export function refreshToken(refreshToken) {
	return http({
		url: BASE_CONTENT + '/refreshToken',
		method: 'put',
		data: http.jsonData({
			'refreshToken': refreshToken
		})
	})
}

/** 退出登录 **/
export function logout() {
	return http({
		url: BASE_CONTENT + '/logout',
		method: 'get',
		params: http.queryData()
	})
}
